/**
 * 移动系统 - 处理实体位置更新
 */
import { System, World } from '../ecs';
import { Transform } from '../components';

export class MovementSystem implements System {
  priority = 10;

  update(deltaTime: number, world: World): void {
    const { width, height } = world.resources;

    for (const entity of world.entities) {
      const transform = entity.getComponent<Transform>('transform');
      if (!transform) continue;

      // 更新位置
      transform.x += transform.velocityX * deltaTime;
      transform.y += transform.velocityY * deltaTime;

      // 边界检查 - 玩家飞机限制在屏幕内
      if (entity.hasComponent('player')) {
        transform.x = Math.max(20, Math.min(width - 20, transform.x));
        transform.y = Math.max(20, Math.min(height - 20, transform.y));
      }

      // 子弹和敌机超出屏幕边界时销毁
      if (entity.hasComponent('bullet') || entity.hasComponent('enemy')) {
        if (transform.x < -50 || transform.x > width + 50 ||
            transform.y < -50 || transform.y > height + 50) {
          entity.destroy();
        }
      }
    }
  }
}